package com.luhuiguo.sso.entity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.tapestry5.beaneditor.NonVisual;

@Entity
@Table(name = "t_persona")
public class Persona extends AuditableEntity {

	private static final long serialVersionUID = 9076466832495405435L;

	@NonVisual
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@NonVisual
	@ManyToOne
	private User user;

	private String name;

	private String nickname;

	private String email;

	private String fullname;

	@Temporal(TemporalType.DATE)
	private Date bod;

	private String gender;

	private String postcode;

	private String country;

	private String language;

	private String timezone;

	@OneToMany(mappedBy = "persona", cascade = CascadeType.ALL)
	private Set<Attribute> attributes;

	public Persona() {
		super();
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public Date getBod() {
		return bod;
	}

	public void setBod(Date bod) {
		this.bod = bod;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getPostcode() {
		return postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getTimezone() {
		return timezone;
	}

	public void setTimezone(String timezone) {
		this.timezone = timezone;
	}

	public Set<Attribute> getAttributes() {
		return attributes;
	}

	public void setAttributes(Set<Attribute> attributes) {
		this.attributes = attributes;
	}

	public Map<String, String> getAttributesAsMap() {
		Map<String, String> attribMap = new HashMap<String, String>();
		if (attributes != null) {
			for (Attribute attribute : attributes) {
				attribMap.put(attribute.getName(), attribute.getValue());
			}

		}
		return attribMap;
	}

	@Override
	public boolean equals(Object other) {
		if (!(other instanceof Persona)) {
			return false;
		}
		Persona castOther = (Persona) other;
		return new EqualsBuilder().append(this.getId(), castOther.getId())
				.isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(getId()).toHashCode();
	}

	@Override
	public String toString() {
		ToStringBuilder sb = new ToStringBuilder(this,
				ToStringStyle.SIMPLE_STYLE).append("name", this.name).append(
				"nickname", this.nickname);
		return sb.toString();
	}

}
